import copy
import json
from utils.environments_json import urlHead
from utils.common_dcc import response_time

# /topicapi/highFrequencyExamPoint/front/classifyByChapter
def classifyByChapterRequest(uri:str,data:str,session):
    url=urlHead+uri
    res = session.post(url=url, data=data.encode("utf-8"))
    resDic=json.loads(res.text)
    response_time(uri, res)
    return resDic, res

# /topicapi/highFrequencyExamPoint/front/listTopic
def listTopicRequest(uri:str,examPointId:int,data:dict,session,isDone=0,isWrongModel=0):
    url = urlHead + uri
    data["examPointId"]=examPointId
    data["isDone"]=isDone
    data["isWrongModel"]=isWrongModel
    res=session.get(url=url, params=data)
    resDic = json.loads(res.text)
    response_time(uri, res)
    return resDic, res


# /topicapi/highFrequencyExamPoint/front/submit
def submitRequest(uri:str,chapterId,data:dict,res:dict,session,answers=None):
    url = urlHead + uri
    otopicList= data["otopicList"][0]
    res_otopicList=res["data"]["otopics"]
    otopicList_req=[]
    if answers==None:
        answers={"otopics":{"danxuan":"D","duoxuan":"D"}}

    # 生成客观题作答
    if res_otopicList != None and res_otopicList != []:
        for otopic in res_otopicList:
            otopicTmp1 = copy.deepcopy(otopicList)
            if otopic["optionType"] == 1:
                otopicTmp1["topicId"] = otopic["topicId"]
                otopicTmp1["answer"] = answers["otopics"]["danxuan"]
                otopicTmp1["costTime"] = 10
                otopicTmp1["isAnswer"] = 1
                otopicTmp1["topicNum"] = otopic["number"]
            if otopic["optionType"] == 2:
                otopicTmp1["topicId"] = otopic["topicId"]
                otopicTmp1["answer"] = answers["otopics"]["duoxuan"]
                otopicTmp1["costTime"] = 10
                otopicTmp1["isAnswer"] = 1
                otopicTmp1["topicNum"] = otopic["number"]
            otopicList_req.append(otopicTmp1)
        data["chapterId"]=chapterId
        data["otopicList"]=otopicList_req
        res=session.post(url=url, data=json.dumps(data).encode("utf-8"))
        resDic = json.loads(res.text)
        response_time(uri, res)
        return resDic, res


# /userapi/userFront/updateGlobalSetting
def setGlogbalSetting(uri,session,data):
    url = urlHead + uri
    res = session.post(url=url, data=json.dumps(data).encode("utf-8"))
    resDic = json.loads(res.text)
    response_time(uri, res)
    return resDic, res


#/userapi/userFront/globalSetting
def getGlogbalSetting(uri,session):
    url = urlHead + uri
    res = session.get(url=url)
    resDic = json.loads(res.text)
    response_time(uri, res)
    return resDic, res


#/topicapi/tmsExam/front/frontListExamRecord
def getPointRecordList(uri,session,data):
    url = urlHead + uri
    res = session.post(url=url, data=json.dumps(data).encode("utf-8"))
    resDic = json.loads(res.text)
    response_time(uri, res)
    return resDic, res




